/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedBean;

import Merlion.SCM.bean.InventoryItemsBeanRemote;
import Merlion.SCM.bean.InventoryMovementsManagementBeanRemote;
import Merlion.util.state.DeliveryOrderState;
import Merlion.util.state.PickingOrderEntryState;
import Merlion.util.state.PickingOrderState;

import Merlion.util.state.WarehouseState;
import java.io.IOException;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import org.primefaces.event.RowEditEvent;

/**
 *
 * @author anindya
 */
@ManagedBean(name = "PickingOrderManagedBean")
@ViewScoped
public class PickingOrderManagedBean implements Serializable {

    private PickingOrderState pickingOrder;
    private List<PickingOrderState> pickingOrderList;
    private PickingOrderState selectedPickingOrder;
    private PickingOrderState selectedViewPickingOrder;
    private PickingOrderState newPickingOrder = new PickingOrderState();
    private PickingOrderState[] selectedPickingOrders;
    private List<WarehouseState> warehouses;
    private List<PickingOrderEntryState> selectedProductList;
    private List<DeliveryOrderState> deliveryOrders;
    private boolean editMode;
    private PickingOrderEntryState selectedPickingOrderEntryState;

    /** Creates a new instance of PickingOrderManagedBean */
    public PickingOrderManagedBean() {
    }
    @EJB
    private InventoryMovementsManagementBeanRemote imm;
    @EJB
    private InventoryItemsBeanRemote iib;

    public void initView(PhaseEvent event) {
        FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        if (pickingOrderList == null) {
            pickingOrderList = imm.viewPickingOrders();
        }
    }

    public List<DeliveryOrderState> getDeliveryOrders() {
        if (deliveryOrders == null) {
            deliveryOrders = imm.viewDeliveryOrders();
        }
        return deliveryOrders;
    }

    public void setDeliveryOrders(List<DeliveryOrderState> deliveryOrders) {
        this.deliveryOrders = deliveryOrders;
    }

    public void addPickingOrder(ActionEvent event) {
        System.out.println("WEB:: Add picking order");

        Long doId = newPickingOrder.getDeliveryOrderId();
        String pRoute = newPickingOrder.getPickingRoute();
        String pMethod = newPickingOrder.getPickingMethod();
        Date issueDate = newPickingOrder.getIssueDate();
        Date dueDate = newPickingOrder.getDueDate();
        Long warehouseId = newPickingOrder.getTargetWarehouseId();

        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("pickingOrders.xhtml");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public void onEditLineEntry(RowEditEvent event) {

        System.out.println("On edit line entry picking order");
        PickingOrderEntryState entry = (PickingOrderEntryState) event.getObject();

        String eStatus = entry.getStatus();
        Long warehouseID = entry.getWarehouseId();
        Date pickingDate = entry.getPickingDate();
        int requestedQty = entry.getRequestedQty();
        int pickingQty = entry.getPickingQty();
        Long id = entry.getId();

        System.out.println("Calling edit picking order entry");
        imm.editPickingOrderEntry(id, eStatus, warehouseID, pickingDate);
    }

    public void onEditRowPickingOrder(RowEditEvent event) {
        System.out.println("WEB :: On edit row picking order");
        PickingOrderState p = (PickingOrderState) event.getObject();

        Long pickId = p.getId();
        String route = p.getPickingRoute();
        String method = p.getPickingMethod();
        Date issueDate = p.getIssueDate();
        Date dueDate = p.getDueDate();
        Long warehouseId = p.getTargetWarehouseId();
        String status = p.getStatus();

        System.out.println("Calling update picking order");
        imm.updatePickingOrder(pickId, route, method, issueDate, dueDate, warehouseId, status);

    }

    public void editPickingOrder(ActionEvent event) {

        if (selectedViewPickingOrder != null) {
            System.out.println("Selected view picking order not null");
            Long pickId = selectedViewPickingOrder.getId();
            String route = selectedViewPickingOrder.getPickingRoute();
            String method = selectedViewPickingOrder.getPickingMethod();
            Date issueDate = selectedViewPickingOrder.getIssueDate();
            Date dueDate = selectedViewPickingOrder.getDueDate();
            Long warehouseId = selectedViewPickingOrder.getTargetWarehouseId();
            String status = selectedViewPickingOrder.getStatus();

            System.out.println("Calling update picking order");
            imm.updatePickingOrder(pickId, route, method, issueDate, dueDate, warehouseId, status);
        }

        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("pickingOrders.xhtml");
        } catch (IOException ex) {
            ex.printStackTrace();
        }

    }

    public void deletePickingOrder(ActionEvent event) {

        System.out.println("WEB:: Delete picking order");
        if (selectedPickingOrder != null) {
            System.out.println("Calling withdraw picking order");
            imm.withdrawPickingOrder(selectedPickingOrder.getId());
        }

        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("pickingOrders.xhtml");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public void deletePickingOrders() {
        System.out.println(":::WEB::: Delete Picking Orders");

        if (selectedPickingOrders != null) {
            System.out.println(":::WEB::: If selected picking orders not null");
            for (int i = 0; i < selectedPickingOrders.length; i++) {
                System.out.println("selectedPickingOrders i: " + i + " - " + selectedPickingOrders[i].getId());
                imm.withdrawPickingOrder(selectedPickingOrders[i].getId());
            }
        } else {
            System.out.println(":::WEB::: Selected Picking orders are null");
        }

        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("pickingOrders.xhtml");
        } catch (IOException ex) {
            ex.printStackTrace();
        }

    }

    public void executePickingOrder(ActionEvent event) {
        if (selectedViewPickingOrder != null) {
            System.out.println("Execute picking order");
            imm.executePickingOrder(selectedViewPickingOrder.getId());
        }
        
        
        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("pickingOrders.xhtml");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public void cancelDelete() {
        for (PickingOrderState sdState : selectedPickingOrders) {
            sdState = null;
        }
        if (selectedPickingOrders == null) {
            System.out.println(":::WEB::: Deletion of Picking Orders cancelled");

        } else {
            System.out.println(":::WEB::: Deletion of Picking Orders are not cancelled");
        }

    }

    public boolean isEditMode() {
        return editMode;
    }

    public void setEditMode(boolean editMode) {
        this.editMode = editMode;
    }

    public PickingOrderState getNewPickingOrder() {
        return newPickingOrder;
    }

    public void setNewPickingOrder(PickingOrderState newPickingOrder) {
        this.newPickingOrder = newPickingOrder;
    }

    public PickingOrderState getPickingOrder() {
        return pickingOrder;
    }

    public void setPickingOrder(PickingOrderState pickingOrder) {
        this.pickingOrder = pickingOrder;
    }

    public List<PickingOrderState> getPickingOrderList() {
        return pickingOrderList;
    }

    public void setPickingOrderList(List<PickingOrderState> pickingOrderList) {
        this.pickingOrderList = pickingOrderList;
    }

    public PickingOrderState getSelectedPickingOrder() {
        return selectedPickingOrder;
    }

    public void setSelectedPickingOrder(PickingOrderState selectedPickingOrder) {
        this.selectedPickingOrder = selectedPickingOrder;
    }

    public PickingOrderState getSelectedViewPickingOrder() {
        return selectedViewPickingOrder;
    }

    public void setSelectedViewPickingOrder(PickingOrderState selectedViewPickingOrder) {
        System.out.println("Selected view picking order");
        List<PickingOrderEntryState> pickList = imm.viewPickingOrderEntries(selectedViewPickingOrder.getId());
        this.setSelectedProductList(pickList);
        this.selectedViewPickingOrder = selectedViewPickingOrder;
    }

    public PickingOrderState[] getSelectedPickingOrders() {
        return selectedPickingOrders;
    }

    public void setSelectedPickingOrders(PickingOrderState[] selectedPickingOrders) {
        this.selectedPickingOrders = selectedPickingOrders;
    }

    public List<PickingOrderEntryState> getSelectedProductList() {
        return selectedProductList;
    }

    public void setSelectedProductList(List<PickingOrderEntryState> selectedProductList) {
        System.out.println("SEt selected product List");
        this.selectedProductList = selectedProductList;
    }

    public List<WarehouseState> getWarehouses() {
        if (warehouses == null) {
            warehouses = iib.viewWarehouse();
        }
        return warehouses;
    }

    public void setWarehouses(List<WarehouseState> warehouses) {
        this.warehouses = warehouses;
    }

    public void withdrawPickingOrderEntry(ActionEvent event) {
        System.out.println("Withdraw picking order entry frm picking list");
        imm.removePickingOrderEntryFromPickingList(selectedViewPickingOrder.getId(),
                selectedPickingOrderEntryState.getId());

        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("pickingOrders.xhtml");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public PickingOrderEntryState getSelectedPickingOrderEntryState() {
        return selectedPickingOrderEntryState;
    }

    public void setSelectedPickingOrderEntryState(PickingOrderEntryState selectedPickingOrderEntryState) {
        System.out.println("Set selected picking order entry state");
        this.selectedPickingOrderEntryState = selectedPickingOrderEntryState;
    }
}
